// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
var SpriteLoader = require('RaiSpriteLoader');

cc.Class({
    extends: cc.Component,

    properties: {
        terminals: [cc.Sprite],
        bar: cc.Node,
        arrow: cc.Node,
        status: false,
        hidePosX: 0,
        showTerminal : [String],
        group : 'B'//阵营
    },


    onLoad : function () {
        this.hidePosX = this.bar.x;
    },

    init : function(group){
        this.group = group;
        this.resetAllTerminals();
    },

    onButtonClick : function () {
        this.status = !this.status;
        this.arrow.scaleX = this.status ? 1 : -1;
        this.bar.x = this.status ? 0 : this.hidePosX;
    },

    //设置已显示的终端卡
    setShownTerminal: function (terminalName)
    {
        if(this.showTerminal.length >= this.terminals.length)
            return;
        let terminal = this.terminals[this.showTerminal.length];
        if(this.showTerminal.indexOf(terminalName) >= 0)
            return;//已经添加过了就返回
        this.showTerminal.push(terminalName);
        SpriteLoader.load(this.group + terminalName, sprite => {
            terminal.spriteFrame = sprite;
        });
    },

    //设置已显示的终端卡 终端卡名使用简写
    setShownTerminalSimple: function (simpleName)
    {
        let tcName = this.parseSimpleTcName(simpleName);
        if(tcName)
            this.setShownTerminal(tcName);
    },

    //重置所有终端卡
    resetAllTerminals : function()
    {
        this.showTerminal.length = 0;
        for(i = 0; i < this.terminals.length; i++)
        {
            let terminal = this.terminals[i];
            SpriteLoader.load(this.group + 'unknown', sprite => {
                terminal.spriteFrame = sprite;
            });
        }
    },

    //简写转终端卡名
    parseSimpleTcName: function(simpleName)
    {
        switch(simpleName)
            {
                case 'lb':
                    return 'lineboost';
                break;
                case 'fw':
                    return 'firewall';
                break;
                case 'vc':
                    return 'viruscheck';
                break;
                case 'nf':
                    return 'notfound';
                break;
                case 'rv':
                    return 'randomverification';
                break;
                case 'zda':
                    return 'zerodayattack';
                break;
                break;
                case 'sb':
                    return 'sandbox';
                break;
                default:
                    return '';
            }
    }
});
